package pers.qianyu.month_202102.date_20210216;

/**
 * 516. 最长回文子序列
 * https://leetcode-cn.com/problems/longest-palindromic-subsequence/
 * - 状态压缩
 *
 * @author mizzle rain
 * @date 2021年2月16日22:44:04
 */
public class LongestPalindromeSubseq2 {
    public int longestPalindromeSubseq(String s) {
        char[] arr = s.toCharArray();
        int n = arr.length;
        int[] dp = new int[n];
        for (int i = 0; i < n; i++) dp[i] = 1;
        for (int i = n - 2; i >= 0; i--) {
            int pre = 0;
            for (int j = i + 1; j < n; j++) {
                int t = dp[j];
                if (arr[i] == arr[j]) dp[j] = pre + 2;
                else dp[j] = Math.max(dp[j], dp[j - 1]);
                pre = t;
            }
        }
        return dp[n - 1];
    }
}